package com.my.util.execl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.entity.Curriculum;
import com.my.entity.GradeClass;
import com.my.service.CurriculumService;
import com.my.service.GradeClassService;
import com.my.util.execl.entity.CurriculumExecl;

import java.util.HashMap;
import java.util.Map;

public class CurriculumExeclListener extends AnalysisEventListener<CurriculumExecl> {

    private static final Map<String, Integer> weekMap = new HashMap<String, Integer>(){{
        put("周日", 0);
        put("周一", 1);
        put("周二", 2);
        put("周三", 3);
        put("周四", 4);
        put("周五", 5);
        put("周六", 6);
    }};

    private CurriculumService service;

    private GradeClassService gcService;

    public CurriculumExeclListener(CurriculumService service, GradeClassService gcService) {
        this.service = service;
        this.gcService = gcService;
    }

    @Override
    public void invoke(CurriculumExecl curriculumExecl, AnalysisContext analysisContext) {
        Curriculum curriculum = new Curriculum();
        curriculum.setWeek(weekMap.get(curriculumExecl.getWeekName())).setName(curriculumExecl.getName()).
                setStartTime(curriculumExecl.getStartTime()).setEndTime(curriculumExecl.getEndTime()).setRemarks(curriculumExecl.getRemarks());
        GradeClass grade = gcService.getOne(new QueryWrapper<GradeClass>().eq("name", curriculumExecl.getGradeName()));
        GradeClass gradeClass = gcService.getOne(new QueryWrapper<GradeClass>().eq("name", curriculumExecl.getClassName()).eq("parent_id", grade.getId()));
        curriculum.setGradeClassId(gradeClass.getId());
        service.save(curriculum);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) { }
}
